home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / ncurses-5.3 / ncurses / tinfo / setbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-27  |  6.0 KB  |  145 lines

  1. /****************************************************************************
  2.  * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
  3.  *                                                                          *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a  *
  5.  * copy of this software and associated documentation files (the            *
  6.  * "Software"), to deal in the Software without restriction, including      *
  7.  * without limitation the rights to use, copy, modify, merge, publish,      *
  8.  * distribute, distribute with modifications, sublicense, and/or sell       *
  9.  * copies of the Software, and to permit persons to whom the Software is    *
  10.  * furnished to do so, subject to the following conditions:                 *
  11.  *                                                                          *
  12.  * The above copyright notice and this permission notice shall be included  *
  13.  * in all copies or substantial portions of the Software.                   *
  14.  *                                                                          *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
  16.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
  18.  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
  19.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
  20.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
  21.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
  22.  *                                                                          *
  23.  * Except as contained in this notice, the name(s) of the above copyright   *
  24.  * holders shall not be used in advertising or otherwise to promote the     *
  25.  * sale, use or other dealings in this Software without prior written       *
  26.  * authorization.                                                           *
  27.  ****************************************************************************/
  28.  
  29. /****************************************************************************
  30.  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  31.  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
  32.  ****************************************************************************/
  33.  
  34. /*
  35. **    setbuf.c
  36. **
  37. **    Support for set_term(), reset_shell_mode(), reset_prog_mode().
  38. **
  39. */
  40.  
  41. #include <curses.priv.h>
  42.  
  43. MODULE_ID("$Id: setbuf.c,v 1.7 2000/12/10 02:55:08 tom Exp $")
  44.  
  45. /*
  46.  * If the output file descriptor is connected to a tty (the typical case) it
  47.  * will probably be line-buffered.  Keith Bostic pointed out that we don't want
  48.  * this; it hoses people running over networks by forcing out a bunch of small
  49.  * packets instead of one big one, so screen updates on ptys look jerky.
  50.  * Restore block buffering to prevent this minor lossage.
  51.  *
  52.  * The buffer size is a compromise.  Ideally we'd like a buffer that can hold
  53.  * the maximum possible update size (the whole screen plus cup commands to
  54.  * change lines as it's painted).  On a 66-line xterm this can become
  55.  * excessive.  So we min it with the amount of data we think we can get through
  56.  * two Ethernet packets (maximum packet size - 100 for TCP/IP overhead).
  57.  *
  58.  * Why two ethernet packets?  It used to be one, on the theory that said
  59.  * packets define the maximum size of atomic update.  But that's less than the
  60.  * 2000 chars on a 25 x 80 screen, and we don't want local updates to flicker
  61.  * either.  Two packet lengths will handle up to a 35 x 80 screen.
  62.  *
  63.  * The magic '6' is the estimated length of the end-of-line cup sequence to go
  64.  * to the next line.  It's generous.  We used to mess with the buffering in
  65.  * init_mvcur() after cost computation, but that lost the sequences emitted by
  66.  * init_acs() in setupscreen().
  67.  *
  68.  * "The setvbuf function may be used only after the stream pointed to by stream
  69.  * has been associated with an open file and before any other operation is
  70.  * performed on the stream." (ISO 7.9.5.6.)
  71.  *
  72.  * Grrrr...
  73.  *
  74.  * On a lighter note, many implementations do in fact allow an application to
  75.  * reset the buffering after it has been written to.  We try to do this because
  76.  * otherwise we leave stdout in buffered mode after endwin() is called.  (This
  77.  * also happens with SVr4 curses).
  78.  *
  79.  * There are pros/cons:
  80.  *
  81.  * con:
  82.  *    There is no guarantee that we can reestablish buffering once we've
  83.  *    dropped it.
  84.  *
  85.  *    We _may_ lose data if the implementation does not coordinate this with
  86.  *    fflush.
  87.  *
  88.  * pro:
  89.  *    An implementation is more likely to refuse to change the buffering than
  90.  *    to do it in one of the ways mentioned above.
  91.  *
  92.  *    The alternative is to have the application try to change buffering
  93.  *    itself, which is certainly no improvement.
  94.  *
  95.  * Just in case it does not work well on a particular system, the calls to
  96.  * change buffering are all via the macro NC_BUFFERED.  Some implementations
  97.  * do indeed get confused by changing setbuf on/off, and will overrun the
  98.  * buffer.  So we disable this by default (there may yet be a workaround).
  99.  */
  100. NCURSES_EXPORT(void)
  101. _nc_set_buffer(FILE * ofp, bool buffered)
  102. {
  103.     /* optional optimization hack -- do before any output to ofp */
  104. #if HAVE_SETVBUF || HAVE_SETBUFFER
  105.     unsigned buf_len;
  106.     char *buf_ptr;
  107.  
  108.     if (getenv("NCURSES_NO_SETBUF") != 0)
  109.     return;
  110.  
  111.     fflush(ofp);
  112.     if ((SP->_buffered = buffered) != 0) {
  113.     buf_len = min(LINES * (COLS + 6), 2800);
  114.     if ((buf_ptr = SP->_setbuf) == 0) {
  115.         if ((buf_ptr = typeMalloc(char, buf_len)) == NULL)
  116.           return;
  117.         SP->_setbuf = buf_ptr;
  118.         /* Don't try to free this! */
  119.     }
  120. #if !USE_SETBUF_0
  121.     else
  122.         return;
  123. #endif
  124.     } else {
  125. #if !USE_SETBUF_0
  126.     return;
  127. #else
  128.     buf_len = 0;
  129.     buf_ptr = 0;
  130. #endif
  131.     }
  132.  
  133. #if HAVE_SETVBUF
  134. #ifdef SETVBUF_REVERSED        /* pre-svr3? */
  135.     (void) setvbuf(ofp, buf_ptr, buf_len, buf_len ? _IOFBF : _IOLBF);
  136. #else
  137.     (void) setvbuf(ofp, buf_ptr, buf_len ? _IOFBF : _IOLBF, buf_len);
  138. #endif
  139. #elif HAVE_SETBUFFER
  140.     (void) setbuffer(ofp, buf_ptr, (int) buf_len);
  141. #endif
  142.  
  143. #endif /* HAVE_SETVBUF || HAVE_SETBUFFER */
  144. }
  145.